package com.xysq.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.CommentVModel;
import com.rockcent.model.CustomerBO;
import com.xysq.XYApplication;
import com.xysq.adapter.EvaluateAdapterVOnePointOne;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;
import com.xysq.util.ToastUtil;
import com.xysq.util.UseUtil;
import com.xysq.util.UserInfoKeeper;
import com.xysq.widget.CircleImageView;
import com.xysq.widget.XYBannerHelper;
import com.xysq.widget.XYBannerInfo;

import java.util.ArrayList;

import butterknife.InjectView;

public class PreviewActivity extends BaseActivity implements XYBannerHelper.OnItemClickListener, View.OnClickListener{

    @InjectView(R.id.img_head)
    CircleImageView headImg;
    @InjectView(R.id.img_sex)
    ImageView sexImg;
    @InjectView(R.id.list_details)
    com.xysq.widget.LoadMoreListView detailsList;
//    @InjectView(R.id.txt_customer_name)
//    TextView nameCustomerTxt;
    @InjectView(R.id.txt_school)
    TextView schoolTxt;
    @InjectView(R.id.txt_name)
    TextView nameTxt;
//    @InjectView(R.id.txt_price)
//    TextView priceTxt;
//    @InjectView(R.id.txt_introduce)
//    TextView introduceTxt;
//    @InjectView(R.id.layout_ad_coupon)
//    RelativeLayout adCouponLayout;
    @InjectView(R.id.ibtn_back)
    ImageButton backIbtn;

    TextView nullDataTxt;
    TextView contentTxt;
    TextView numberTxt;
    TextView titleTxt;
    TextView priceTxt;

    EvaluateAdapterVOnePointOne evaluateAdapterVOnePointOne;
    ArrayList<CommentVModel> commentVModels;
    ArrayList<XYBannerInfo> infos;
    private XYBannerHelper banner;
    private String description;
    private String picUrl;
    private String name;
    private double price;
    private int number;
    String[] picUrls;
    boolean firstActive = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview);
        getExtra();
        initView();
        loadData();
    }

    private void initView() {
        backIbtn.setOnClickListener(this);
        final View header = View.inflate(this, R.layout.fragment_customer_goods_v_one_point_one_head, null);//这是头部内容
        final View bottom = View.inflate(this, R.layout.null_data, null);//这是尾部内容

        nullDataTxt = (TextView) bottom.findViewById(R.id.txt_null_data);
        contentTxt = (TextView) header.findViewById(R.id.txt_content_of_tips);
        titleTxt = (TextView) header.findViewById(R.id.txt_title_of_tips);
        priceTxt = (TextView) header.findViewById(R.id.txt_price);
        numberTxt = (TextView) header.findViewById(R.id.txt_number);

        titleTxt.setText(name);
        priceTxt.setText(UseUtil.handlePrice(price));
        numberTxt.setText("已被打赏" + number + "次");
        contentTxt.setText(description);

        detailsList.addFooterView(bottom);
        detailsList.addHeaderView(header);
        commentVModels = new ArrayList<>();
        evaluateAdapterVOnePointOne = new EvaluateAdapterVOnePointOne(this, commentVModels);
        detailsList.setAdapter(evaluateAdapterVOnePointOne);
        nullDataTxt.setVisibility(View.VISIBLE);
        processPicUrl(picUrls);
    }

    private void loadData() {
        loadCustomer();
    }

    private void loadCustomer() {
        appAction.getCustomer(UserInfoKeeper.readPhoneNumber(this), "", "", new CallbackListener<CustomerBO>() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onSuccess(CustomerBO data) {
                Log.e("++++++", "获取用户信息成功");
                if (!TextUtils.isEmpty(data.getHeadUrl()) && !data.getHeadUrl().equals("") && !data.getHeadUrl().equals("null")) {
                    String imageUrl = data.getHeadUrl();
                    if (imageUrl.startsWith("http://")) {
                        XYApplication.getImageLoader().displayImage(imageUrl, headImg);
                    } else {
                        XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + imageUrl, headImg);
                    }
                }
                nameTxt.setText(UseUtil.selectName(data.getNickname(), data.getName(), data.getPhone()));
                schoolTxt.setText(data.getSchoolName());
                if (null != data.getSex()) {
                    setSexImg(data.getSex());
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                Log.e("++++++", "获取用户信息失败" + message);
                ToastUtil.showShort(PreviewActivity.this, "获取用户信息失败");
            }
        });
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setSexImg(String sex) {
        if (sex.equals("1")) {
            sexImg.setBackground(getResources().getDrawable(R.drawable.ic_nan));
        } else {
            sexImg.setBackground(getResources().getDrawable(R.drawable.ic_nv));
        }
    }

    private void getExtra() {
        Intent intent = getIntent();
        description = intent.getStringExtra("description");
        picUrl = intent.getStringExtra("picUrl");
        name = intent.getStringExtra("name");
        price = intent.getDoubleExtra("price", 0);
        number = 0;
//        number = intent.getIntExtra("number", 0);
//        nameTxt.setText(name);
//        introduceTxt.setText(description);
//        priceTxt.setText("￥" + UseUtil.handlePrice(price));
        picUrls = split(picUrl);
    }

    private void initBanner() {
        banner = new XYBannerHelper(PreviewActivity.this);
        banner.setBannerInfo(infos);
        banner.startAutoRoll();
        banner.setOnItemClickCallback(this);
    }

    private void processPicUrl(String[] picUrls) {
        infos = new ArrayList<>();
        for (int i = 0; i < picUrls.length; i++) {
            infos.add(new XYBannerInfo("", PropertiesUtil.getImageServer() + picUrls[i]));
        }
        initBanner();
    }

    private String[] split(String picUrls) {
        String[] picUrl = picUrls.split(";");
        return picUrl;
    }

    /**
     * 点击事件回调
     *
     * @param index : 被点击的图片列表位置
     * @param v     : 被点击的图片（它是一个ImageView类）
     */
    @Override
    public void onItemClick(int index, View v) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (banner != null) {
            banner.destroyHelper();
        }
    }

    @Override
    protected void onStart(){
        super.onStart();
        if(banner != null && firstActive == false) {
            banner.startAutoRoll();
        }
        firstActive = false;
    }

    @Override
    protected void onStop(){
        super.onStop();
        if(banner != null){
            banner.stopAutoRoll();
        }
    }

    @Override
    public void onClick(View view) {
        if(view == backIbtn){
            finish();
        }
    }
}
